////////////////////////////////////////////////////////////////////////////////
//
//  MICROSOFT CORPORATION
//  Copyright (c) Microsoft Corporation.
//  All Rights Reserved.
//
//  NOTICE: Microsoft Confidential. Intended for Internal Use Only.
//
////////////////////////////////////////////////////////////////////////////////

package com.msn.beet.utils
{

import com.msn.beet.core.StaticClass;

import flash.utils.describeType;

/**
 * Utility for printing the properties and values of non-dynamic classes as
 * an xml tree. Currently only prints one level of children.
 */
public class XMLSerializer extends StaticClass
{
    include "../core/Version.as";

    /**
    * Builds xml tree of properties, strips out superfluous information.
    */
    public static function serialize(object:Object):XML
    {
        var serializedObject:XML = describeType(object);
        removeIgnoredNodes(serializedObject);
        getChildValues(object, serializedObject);
        return serializedObject;
    }

    private static function removeIgnoredNodes(serializedObject:XML):void
    {
        delete serializedObject.method;
        delete serializedObject.implementsInterface;
        delete serializedObject.extendsClass;
        delete serializedObject.factory;
        delete serializedObject.extendsClass
    }

    private static function getChildValues(object:Object,
                                           serializedObject:XML):void
    {
        var valueChildren:XMLList = serializedObject.variable;
        valueChildren += serializedObject.accessor;
        var n:uint = valueChildren.length();
        for (var i:uint = 0; i < n; i++)
        {
            valueChildren[i].@value = object[valueChildren[i].@name];
            deleteChildren(valueChildren[i]);
            delete valueChildren[i].@access;
            delete valueChildren[i].@declaredBy;
        }
    }

    private static function deleteChildren(node:XML):void
    {
        var n:int = node.length();
        for (var i:uint = 0; i < n; i++)
        {
            delete node.children()[i];
        }
    }
}

}
